<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账号状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择账号状态"
          clearable
          style="width: 100%;"
        >
          <el-option
            v-for="(value, key) in statusInfo"
            :key="key"
            :label="value"
            :value="key"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:operationAccount:add']"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="operationAccountList">
      <el-table-column :formatter="defaultTableColumnDate" label="序号" align="center" type="index" />
      <el-table-column :formatter="defaultTableColumnDate" label="手机号" align="center" prop="phone" />
      <el-table-column :formatter="defaultTableColumnDate" label="姓名" align="center" prop="userName" />
      <el-table-column :formatter="defaultTableColumnDate" label="分配角色" align="center" prop="roleNames" />
      <el-table-column :formatter="defaultTableColumnDate" label="更新人" align="center" prop="updateBy" />
      <el-table-column :formatter="defaultTableColumnDate" label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :formatter="defaultTableColumnDate" label="状态" align="center" prop="status">
        <template slot-scope="scope">
          {{ statusInfo[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column :formatter="defaultTableColumnDate" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:operationAccount:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:operationAccount:edit']"
          >{{scope.row.status=='0'?'禁用':'启用'}}</el-button>
          <el-button
            size="mini"
            type="text"
            @click="resetPwd(scope.row)"
            v-hasPermi="['system:operationAccount:edit']"
          >重置密码</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改运营账号对话框 -->
    <el-dialog destroy-on-close :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="登录密码" prop="password" v-if="!form.id">
          <el-input v-model="form.password" placeholder="请输入登录密码" />
        </el-form-item>
        <el-form-item label="选择角色" prop="roleList">
          <el-checkbox-group v-model="form.roleList">
            <el-checkbox v-for="item in roleList" :label="item.id" :key="item.id">{{ item.roleName }}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOperationAccount, getOperationAccount, addOperationAccount, updateOperationAccount, operationRoleList, resetPassword } from "@/api/system/operationAccount";

export default {
  name: "OperationAccount",
  data() {
    return {
      roleList: [],
      statusInfo: {
        '0': '启用',
        '1': '禁用',
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 运营账号表格数据
      operationAccountList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        phone: null,
        userName: null,
        password: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getRoleList()
  },
  methods: {
    getRoleList() {
      operationRoleList({}).then(response => {
        this.roleList = response.data
      });
    },
    /** 查询运营账号列表 */
    getList() {
      this.loading = true;
      listOperationAccount(this.queryParams).then(response => {
        this.operationAccountList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        phone: null,
        userName: null,
        roleList: [],
        password: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加运营账号";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOperationAccount(id).then(response => {
        this.form = response.data;
        delete this.form.password
        this.open = true;
        this.title = "修改运营账号";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOperationAccount(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOperationAccount(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认修改运营账号编号为"' + id + '"的数据项的状态？').then(function() {
        return updateOperationAccount({id, status: row.status=='0'?'1':'0'});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("修改成功");
      }).catch(() => {});
    },
    resetPwd(row) {
      const id = row.id;
      this.$prompt('请输入新密码', '账号权限管理 >  重置密码', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPattern: /^(?=.*[a-zA-Z])(?=.*[0-9])[a-zA-Z0-9]{6,12}$/,
          inputErrorMessage: '密码长度需为6-12位的字母和数字，区分大小写'
        }).then(async ({ value }) => {
          await resetPassword({ id, password: value })
          this.$modal.msgSuccess("修改成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/operationAccount/export', {
        ...this.queryParams
      }, `operationAccount_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
